package com.tebiecloud.common.mybatis;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;

import java.util.HashMap;

/**
 * 自定义Map
 * @Author: tebie
 * @Date: 2019-08-01 13:50
 */
public class EntityMap extends HashMap<String, Object> {

    private static final long serialVersionUID = 1L;
    public static EnumConvertInterceptor interceptors = null;

    public EntityMap() {

    }

    /**
     * 字符串处理获取key值，例：user.name -> name
     * @param field
     * @return
     */
    private static String getField(String field) {
        String str = "";
        int s = field.indexOf(".");
        if (s > -1) {
            str = field.substring(s + 1);
        } else {
            str = field;
        }
        return str;
    }

    /**
     * 设置字典拦截器
     * @param interceptor
     */
    public static void setEnumConvertInterceptor(EnumConvertInterceptor interceptor) {
        interceptors = interceptor;
    }

    /**
     * 设置key值对应value
     * @param key
     * @param value
     * @return
     */
    public EntityMap put(String key, Object value) {
        /* List<Object> dictKeys = redisUtils.getList("DICT_KEYS");
         *//*判断字段是否是字典类型*//*
        if (dictKeys.contains(key) && ObjectUtils.isNotEmpty(value)) {
            Object dictValue = dataMaps.get(key + "_" + value.toString());
            *//*返回数据中添加字典显示值*//*
            super.put(key + "Title", dictValue);
        }*/
        if (ObjectUtils.isNotEmpty(interceptors)) {
            interceptors.convert(this, key, value);
        }
        if (ObjectUtils.isNotNull(value)) {
            super.put(key, value);
        } else {
            super.put(key, "");
        }
        return this;
    }

    /**
     * 获取key对应的value
     * @param key
     * @param <T>
     * @return
     */
    public <T> T get(String key) {
        return get(key, null);
    }

    /**
     * 获取key对应的value
     * @param key
     * @param def 不存在时返回默认值
     * @param <T>
     * @return
     */
    public <T> T get(String key, T def) {
        Object obj = super.get(key);
        if (ObjectUtils.isNotEmpty(obj)) {
            return (T) obj;
        } else {
            return def;
        }
    }

}
